package cn.uexpo.lhz.common.utils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;



/**
 * 服务器返回给客户端的JSON格式的数据
 *
 */
public class JsonData extends HashMap<String, Object> implements Serializable {
	private static final long serialVersionUID = -8855960778711040221L;

	private static final String CODE_KEY = "code";// 操作代码 200 成功 非200 失败
	private static final String MESSAGE_KEY = "message";// 操作消息
	private static final String DATA = "data";// 结果集
	private static final String PAGE_KEY = "page";// 当前页码  pageNum
	private static final String ROWS_KEY = "rows";// 每页行数/页大小  pageSize
	private static final String TOTAL_KEY = "total";// 总记录数  TotalSize
	private static final String TS_KEY = "ts";// 时间戳

	public JsonData() {
		super();
		this.put(CODE_KEY, 200);// 默认操作成功
		this.put(TS_KEY, System.currentTimeMillis());
	}

	public void setCode(Integer code) {
		this.put(CODE_KEY, code);
	}

	public void setMessage(String message) {
		this.put(MESSAGE_KEY, message);
	}

	public void setData(Object data) {
		this.put(DATA, data);
	}

	public void setPage(Integer page) {
		this.put(PAGE_KEY, page);
	}

	public void setRows(Integer rows) {
		this.put(ROWS_KEY, rows);
	}

	public void setTotal(Integer total) {
		this.put(TOTAL_KEY, total);
	}
	public void setTotal(Long total){
		this.put(TOTAL_KEY,total);
	}

	/**
	 * 不会返回数据结果集
	 * @param msg
	 * @param code
	 * @param success
	 * @return
	 */
	public static Map toJsonMessage(String msg, String code, boolean success){
		Map<String,Object> json=new HashMap<>();
		json.put("msg",msg);
		json.put("code",code);
		json.put("success",success);
		return json;
	}

	/**
	 * 会返回数据结果集
	 * @param msg
	 * @param code
	 * @param success
	 * @param data
	 * @return
	 */
	public static Map toJsonObject(String msg,String code,boolean success,Object data){
		Map<String,Object> json=new HashMap<>();
		json.put("msg",msg);
		json.put("code",code);
		json.put("success", success);
		json.put("data", data);
		return json;
	}


}
